package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderGoodsMapper;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.AdminOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


@Service
public class AdminOrderServiceImpl implements AdminOrderService {

    @Autowired
    private DtsOrderMapper dtsOrderMapper;

    @Autowired
    private DtsOrderGoodsMapper dtsOrderGoodsMapper;

    @Autowired
    private DtsUserMapper dtsUserMapper;


    @Override
    public IPage<DtsOrder> selectOrderList(Integer page, Integer limit, Integer userId, String orderSn, Integer[] orderStatusArray, String sort, String order) {

        IPage<DtsOrder> iPage = new Page<>(page,limit);

        QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();
        wrapper.eq(userId != null, "user_id", userId);
        wrapper.like(!StringUtils.isEmpty(orderSn), "order_sn", orderSn);

        if (orderStatusArray != null) {
            wrapper.in("order_status", Arrays.asList(orderStatusArray));
        }
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);

        IPage<DtsOrder> result = dtsOrderMapper.selectPage(iPage, wrapper);
        return result;
    }

    @Override
    public List<DtsOrderGoods> selectOrderGoodes(Integer id) {

        QueryWrapper<DtsOrderGoods> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", id);

        List<DtsOrderGoods> dtsOrderGoods = dtsOrderGoodsMapper.selectList(wrapper);

        return dtsOrderGoods;
    }

    @Override
    public DtsOrder selectOrder(Integer id) {

        DtsOrder dtsOrder = dtsOrderMapper.selectById(id);

        return dtsOrder;
    }

    @Override
    public DtsUser selectUser(Integer userId) {

        QueryWrapper<DtsUser> wrapper = new QueryWrapper<>();
        wrapper.select("nickname","avatar");
        wrapper.eq("id", userId);
        DtsUser dtsUser = dtsUserMapper.selectOne(wrapper);

        return dtsUser;
    }

    @Override
    public void updateOrder(DtsOrder dtsOrder) {
        dtsOrder.setShipTime(new Date());
        dtsOrderMapper.updateById(dtsOrder);
    }
}
